import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.LangDataKeys
import com.intellij.openapi.fileEditor.OpenFileDescriptor
import com.intellij.openapi.ui.Messages
import com.intellij.vcsUtil.VcsUtil.getVirtualFile
import java.io.File

class YapiPojoAction : AnAction() {
    override fun actionPerformed(event: AnActionEvent) {
        val actionFolder = event.getData(LangDataKeys.VIRTUAL_FILE)
        val project = event.getData(LangDataKeys.PROJECT)!!
        actionFolder?.canonicalPath?.let { selectedFilePath ->
            val file = File(selectedFilePath)
            val fileGenerated = PojoGen().gen(file)
            //刷新工程目录
            project.baseDir?.refresh(false, true)
            //打开文件
            if (fileGenerated != null) {
                val generateVirtualFile = getVirtualFile(fileGenerated)
                if (generateVirtualFile == null) {
                    Messages.showMessageDialog("请打开YapiPojo.kt", "操作完成", Messages.getInformationIcon())
                } else {
                    OpenFileDescriptor(project, generateVirtualFile).navigate(true)
                }
            }
        }
    }
}